package com.prestamomo.vil.ppa.pmvilpage;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.prestamomo.vil.ppa.R;
import com.prestamomo.vil.ppa.pmvilapi.PmvilBodys;
import com.prestamomo.vil.ppa.pmvilapi.PmvilHttpRequest;
import com.prestamomo.vil.ppa.pmvilapi.PmvilSerConfig;
import com.prestamomo.vil.ppa.pmvilapi.PmvilUserRz;
import com.prestamomo.vil.ppa.pmvilmedium.pmviluse.PmvilTosMd;
import com.prestamomo.vil.ppa.pmvilnet.PmvilAjustinfse;
import com.prestamomo.vil.ppa.pmvilnet.pmvillistener.PmvilCallBack;
import com.prestamomo.vil.ppa.pmvilpage.pmfitter.PmvilSourceFiter;
import com.prestamomo.vil.ppa.pmvilpage.pmrot.PmvilAtyRoot;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PmvilSource2Vy extends PmvilAtyRoot {

    private String pmvilemail;
    private String pmvilFacebook;
    private String pmvilJycode;
    private String pmvilGzcode;
    private String pmvilSrcode;
    private String pmvilYtcode;
    private List<PmvilSerConfig.ExtInfoArrPmvil> pmvilGxList;
    private List<PmvilSerConfig.ExtInfoArrPmvil> pmvilGxList1 = new ArrayList<>();
    private List<PmvilSerConfig.ExtInfoArrPmvil> pmvilGxList2 = new ArrayList<>();
    private List<PmvilSerConfig.ExtInfoArrPmvil> pmvilGxList3 = new ArrayList<>();
    private ImageView source2PmvilIvback;
    private LinearLayout source2PmvilLlrela1;
    private TextView source2PmvilTvrela1;
    private EditText source2PmvilEtname1;
    private TextView source2PmvilTvname1;
    private EditText source2PmvilEtphone1;
    private TextView source2PmvilTvphone1;
    private LinearLayout source2PmvilLlrela2;
    private TextView source2PmvilTvrela2;
    private EditText source2PmvilEtname2;
    private TextView source2PmvilTvname2;
    private EditText source2PmvilEtphone2;
    private TextView source2PmvilTvphone2;
    private LinearLayout source2PmvilLlrela3;
    private TextView source2PmvilTvrela3;
    private EditText source2PmvilEtname3;
    private TextView source2PmvilTvname3;
    private EditText source2PmvilEtphone3;
    private TextView source2PmvilTvphone3;
    private TextView source2PmvilTvtj;
    private String pmvilGxcode1;
    private String pmvilGxcode2;
    private String pmvilGxcode3;

    @Override
    protected int getPmvilLayoutId() {
        return R.layout.source2_pmvil_ly;
    }

    @Override
    protected void showPmvilUi() {
        pmvilemail = getIntent().getStringExtra("pmvil_email");
        pmvilFacebook = getIntent().getStringExtra("pmvil_facebook");
        pmvilJycode = getIntent().getStringExtra("pmvil_jycode");
        pmvilYtcode = getIntent().getStringExtra("pmvil_ytcode");
        pmvilGzcode = getIntent().getStringExtra("pmvil_gzcode");
        pmvilSrcode = getIntent().getStringExtra("pmvil_srcode");
        pmvilGxList = (List<PmvilSerConfig.ExtInfoArrPmvil>) getIntent().getSerializableExtra("pmvil_gxList");
        if (pmvilGxList != null && pmvilGxList.size() != 0) {
            pmvilGxList3.addAll(pmvilGxList);
            for (PmvilSerConfig.ExtInfoArrPmvil extInfoArrPmvil : pmvilGxList) {
                if (extInfoArrPmvil.getPmvilcode().equals("1") || extInfoArrPmvil.getPmvilcode().equals("2")) {
                    pmvilGxList1.add(extInfoArrPmvil);
                } else {
                    pmvilGxList2.add(extInfoArrPmvil);
                }
            }
        }

        source2PmvilIvback = findViewById(R.id.source2_pmvil_iv_back);
        source2PmvilLlrela1 = findViewById(R.id.source2_pmvil_ll_rela1);
        source2PmvilTvrela1 = findViewById(R.id.source2_pmvil_tv_rela1);
        source2PmvilEtname1 = findViewById(R.id.source2_pmvil_et_name1);
        source2PmvilTvname1 = findViewById(R.id.source2_pmvil_tv_name1);
        source2PmvilEtphone1 = findViewById(R.id.source2_pmvil_et_phone1);
        source2PmvilTvphone1 = findViewById(R.id.source2_pmvil_tv_phone1);
        source2PmvilLlrela2 = findViewById(R.id.source2_pmvil_ll_rela2);
        source2PmvilTvrela2 = findViewById(R.id.source2_pmvil_tv_rela2);
        source2PmvilEtname2 = findViewById(R.id.source2_pmvil_et_name2);
        source2PmvilTvname2 = findViewById(R.id.source2_pmvil_tv_name2);
        source2PmvilEtphone2 = findViewById(R.id.source2_pmvil_et_phone2);
        source2PmvilTvphone2 = findViewById(R.id.source2_pmvil_tv_phone2);
        source2PmvilLlrela3 = findViewById(R.id.source2_pmvil_ll_rela3);
        source2PmvilTvrela3 = findViewById(R.id.source2_pmvil_tv_rela3);
        source2PmvilEtname3 = findViewById(R.id.source2_pmvil_et_name3);
        source2PmvilTvname3 = findViewById(R.id.source2_pmvil_tv_name3);
        source2PmvilEtphone3 = findViewById(R.id.source2_pmvil_et_phone3);
        source2PmvilTvphone3 = findViewById(R.id.source2_pmvil_tv_phone3);
        source2PmvilTvtj = findViewById(R.id.source2_pmvil_tv_tj);

        source2PmvilTvname1.setOnClickListener(v -> startCcteTxlu(0));
        source2PmvilTvname2.setOnClickListener(v -> startCcteTxlu(1));
        source2PmvilTvname3.setOnClickListener(v -> startCcteTxlu(2));
        source2PmvilTvphone1.setOnClickListener(v -> startCcteTxlu(0));
        source2PmvilTvphone2.setOnClickListener(v -> startCcteTxlu(1));
        source2PmvilTvphone3.setOnClickListener(v -> startCcteTxlu(2));
    }

    private void startCcteTxlu(int startCcteType) {
        try {
            Intent pickCcteIntent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
            startActivityForResult(pickCcteIntent, startCcteType);
        }catch (Exception e){
            hindeCcteTvs();
        }

    }

    private void hindeCcteTvs() {
        PmvilTosMd.showPmvaMsg("請手動塡寫");
        source2PmvilTvname1.setVisibility(View.GONE);
        source2PmvilTvname2.setVisibility(View.GONE);
        source2PmvilTvname3.setVisibility(View.GONE);
        source2PmvilTvphone1.setVisibility(View.GONE);
        source2PmvilTvphone2.setVisibility(View.GONE);
        source2PmvilTvphone3.setVisibility(View.GONE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            showCctePhones(requestCode,data);
        }
    }

    private void showCctePhones(int cctePickType,Intent ccteData) {
        try{
            Uri contactCcteUri = ccteData.getData();
            String[] queryCcteFields = new String[]{"data1", "display_name"};
            Cursor ccteCusr =getContentResolver().query(contactCcteUri, queryCcteFields, null, null, null);
            if (ccteCusr == null) {
                hindeCcteTvs();
            }else {
                try {
                    if (ccteCusr.getCount() == 0) {
                        hindeCcteTvs();
                        return;
                    }
                    ccteCusr.moveToNext();
                    if(cctePickType==0){
                        source2PmvilEtname1.setText(ccteCusr.getString(1));
                        source2PmvilEtphone1.setText(ccteCusr.getString(0));
                        source2PmvilTvname1.setVisibility(View.GONE);
                        source2PmvilTvphone1.setVisibility(View.GONE);


                    }else if(cctePickType==1){
                        source2PmvilEtname2.setText(ccteCusr.getString(1));
                        source2PmvilEtphone2.setText(ccteCusr.getString(0));
                        source2PmvilTvname2.setVisibility(View.GONE);
                        source2PmvilTvphone2.setVisibility(View.GONE);

                    }else if(cctePickType==2){
                        source2PmvilEtname3.setText(ccteCusr.getString(1));
                        source2PmvilEtphone3.setText(ccteCusr.getString(0));
                        source2PmvilTvname3.setVisibility(View.GONE);
                        source2PmvilTvphone3.setVisibility(View.GONE);
                    }
                } finally {
                    ccteCusr.close();
                }
            }
        }catch (Exception e){
            hindeCcteTvs();
        }
    }
    @Override
    protected void clickePmvil() {
        source2PmvilIvback.setOnClickListener(v -> showPmvilWaliuDlg());
        source2PmvilLlrela1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSource2PmvilDgl(1, source2PmvilLlrela1, source2PmvilTvrela1, "關係", pmvilGxList1);
            }
        });
        source2PmvilLlrela2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSource2PmvilDgl(2, source2PmvilLlrela2, source2PmvilTvrela2, "關係", pmvilGxList2);
            }
        });
        source2PmvilLlrela3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSource2PmvilDgl(3, source2PmvilLlrela3, source2PmvilTvrela3, "關係", pmvilGxList3);
            }
        });

        source2PmvilTvtj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(source2PmvilTvrela1.getText()) || TextUtils.isEmpty(source2PmvilEtname1.getText()) || TextUtils.isEmpty(source2PmvilEtphone1.getText())
                        || TextUtils.isEmpty(source2PmvilTvrela2.getText()) || TextUtils.isEmpty(source2PmvilEtname2.getText()) || TextUtils.isEmpty(source2PmvilEtphone2.getText())
                        || TextUtils.isEmpty(source2PmvilTvrela3.getText()) || TextUtils.isEmpty(source2PmvilEtname3.getText()) || TextUtils.isEmpty(source2PmvilEtphone3.getText())) {
                    PmvilTosMd.showPmvaMsg("資訊不完整");
                } else if (source2PmvilEtphone1.getText().toString().equals(source2PmvilEtphone2.getText().toString()) ||
                        source2PmvilEtphone1.getText().toString().equals(source2PmvilEtphone3.getText().toString()) ||
                        source2PmvilEtphone3.getText().toString().equals(source2PmvilEtphone2.getText().toString())) {
                    PmvilTosMd.showPmvaMsg("號碼不能相同");
                } else {
                    sendAdjPmvilEvent(PmvilAjustinfse.pmvil_dbergsefe);
                    sendAdjPmvilEvent(PmvilAjustinfse.pmvil_dbergsefe_0);
                    Map<String, Object> pmvilAuthMaps = new HashMap<>();
                    pmvilAuthMaps.put("yqrmeu", pmvilJycode);//education
                    pmvilAuthMaps.put("eoeeir", pmvilYtcode);//userType
                    pmvilAuthMaps.put("ipbtynw", pmvilGzcode);//workType
                    pmvilAuthMaps.put("hfsjjtf", pmvilSrcode);//monthIncome
                    pmvilAuthMaps.put("wmzmgsmof", pmvilemail);//email
                    pmvilAuthMaps.put("line", pmvilFacebook);//line
                    Map<String, Object> pmvilrela1 = new HashMap<>();
                    pmvilrela1.put("yvmxvexq", pmvilGxcode1);//relationship
                    pmvilrela1.put("ygsqna", source2PmvilEtname1.getText().toString());//contactsName
                    pmvilrela1.put("aawuocibk", source2PmvilEtphone1.getText().toString());//contactsMobile
                    Map<String, Object> pmvilrela2 = new HashMap<>();
                    pmvilrela2.put("yvmxvexq", pmvilGxcode2);//relationship
                    pmvilrela2.put("ygsqna", source2PmvilEtname2.getText().toString());//contactsName
                    pmvilrela2.put("aawuocibk", source2PmvilEtphone2.getText().toString());//contactsMobile
                    Map<String, Object> pmvilrela3 = new HashMap<>();
                    pmvilrela3.put("yvmxvexq", pmvilGxcode3);//relationship
                    pmvilrela3.put("ygsqna", source2PmvilEtname3.getText().toString());//contactsName
                    pmvilrela3.put("aawuocibk", source2PmvilEtphone3.getText().toString());//contactsMobile
                    List<Map<String, Object>> pmvilRelaList = new ArrayList<>();
                    pmvilRelaList.add(pmvilrela1);
                    pmvilRelaList.add(pmvilrela2);
                    pmvilRelaList.add(pmvilrela3);
                    pmvilAuthMaps.put("hzclbuem", pmvilRelaList);//cashUserContacts
                    postUserPmvilAuth(pmvilAuthMaps);
                }
            }
        });
    }

    @Override
    protected void loadPmvilData() {

    }

    private void getPmvilQueryUser() {
        showPmvilLoading();
        PmvilHttpRequest.getPmvilQueryUser(this,  new PmvilCallBack<PmvilUserRz>() {

            @Override
            public void onPmvilSucc(String pmvilMethod, PmvilUserRz pmvilContent) {
                if (pmvilContent.getMatchResult().equals("0")) {
                    sendAdjPmvilEvent(PmvilAjustinfse.pmvil_bsgrsfgrdd_t);
                    sendAdjPmvilEvent(PmvilAjustinfse.pmvil_bsgrsfgrdd_t_0);
                    Intent pmvilIntent = new Intent(pmvilAy, PmvilPlaceVy.class);
                    if (!pmvilContent.getLivenessStatus()) {
                        pmvilIntent.putExtra("pmvil_isface", true);
                    } else {
                        pmvilIntent.putExtra("pmvil_isface", false);
                    }
                    pmvilIntent.putExtra("pmvil_ischange", true);
                    startActivity(pmvilIntent);
                    finish();
                }
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmvilUserRz pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmvilUserRz pmvilCnt, String pmvilCode, String pmvilMsg) {
                tostPmvilErrMsg(pmvilMsg);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_ogarfdbdfgt_e);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_ogarfdbdfgt_e_0);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                tostPmvilErrMsg(pmvilError);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_ogarfdbdfgt_e);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_ogarfdbdfgt_e_0);
            }
        });
    }

    private void postUserPmvilAuth(Map<String, Object> pmvilAuthMap) {
        showPmvilLoading();
        PmvilHttpRequest.postUserPmvilAuth(this, pmvilAuthMap, new PmvilCallBack<PmvilBodys>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmvilBodys pmvilContent) {
                if (pmvilContent.getPmvilresCode().equals("200")) {
                    sendAdjPmvilEvent(PmvilAjustinfse.pmvil_bsgrsfgrdd_t);
                    sendAdjPmvilEvent(PmvilAjustinfse.pmvil_bsgrsfgrdd_t_0);
                    //getCheckPmvilPage(true);
                    getPmvilQueryUser();
                } else {
                    tostPmvilErrMsg("發送資料錯誤");
                }
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmvilBodys pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmvilBodys pmvilCnt, String pmvilCode, String pmvilMsg) {
                tostPmvilErrMsg(pmvilMsg);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_ogarfdbdfgt_e);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_ogarfdbdfgt_e_0);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                tostPmvilErrMsg(pmvilError);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_ogarfdbdfgt_e);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_ogarfdbdfgt_e_0);
            }
        });
    }

    private void showSource2PmvilDgl(int pmvilType, LinearLayout sourcePmvilLl, TextView sourcePmvilTv, String sourcePmvilStr, List<PmvilSerConfig.ExtInfoArrPmvil> pmvilSourc) {
        View soucrPmvilView = LayoutInflater.from(this).inflate(R.layout.source_pmvil_pop_ly, null, false);
        final PopupWindow soucrPmvilPop = new PopupWindow(soucrPmvilView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        soucrPmvilPop.setTouchable(true);
        soucrPmvilPop.showAsDropDown(sourcePmvilLl);
        ImageView soucrPmvilPopIvback = soucrPmvilView.findViewById(R.id.source_pmvil_pop_iv_back);
        TextView soucrPmvilPopTvtitle = soucrPmvilView.findViewById(R.id.source_pmvil_pop_tv_title);
        RecyclerView soucrPmvilPopRecycle = soucrPmvilView.findViewById(R.id.source_pmvil_pop_recycle);
        soucrPmvilPopTvtitle.setText(sourcePmvilStr);
        soucrPmvilPopIvback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                soucrPmvilPop.dismiss();
            }
        });
        PmvilSourceFiter pmvilSourceFiter = new PmvilSourceFiter(pmvilAy, pmvilSourc);

        soucrPmvilPopRecycle.setAdapter(pmvilSourceFiter);
        pmvilSourceFiter.setSourceFilerIngter(new PmvilSourceFiter.SourceFilerIngter() {
            @Override
            public void setPmvilSourCheckBean(PmvilSerConfig.ExtInfoArrPmvil pmvilSourPopBen) {
                sourcePmvilTv.setText(pmvilSourPopBen.getPmvilname());
                if (pmvilType == 1) {
                    pmvilGxcode1 = pmvilSourPopBen.getPmvilcode();
                } else if (pmvilType == 2) {
                    pmvilGxcode2 = pmvilSourPopBen.getPmvilcode();
                } else if (pmvilType == 3) {
                    pmvilGxcode3 = pmvilSourPopBen.getPmvilcode();
                }
                soucrPmvilPop.dismiss();
            }
        });
    }

    @Override
    public void onBackPressed() {
        showPmvilWaliuDlg();
    }
}
